package com.example.smartstart.controller;

import com.example.smartstart.entity.Point;
import com.example.smartstart.model.MyResponseData;
import com.example.smartstart.model.Result;
import com.example.smartstart.entity.Student;
import com.example.smartstart.entity.StudentInfo;
import com.example.smartstart.service.IPointService;
import com.example.smartstart.service.IStudentInfoService;
import com.example.smartstart.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("student/")
public class StudentController {
    @Autowired
    private IStudentService iStudentService;

    @Autowired
    private IStudentInfoService iStudentInfoService;

    @Autowired
    private IPointService iPointService;

    // 注册
    @PostMapping("register")
    public Result register(@RequestBody StudentInfo studentInfo){
        String examinerNumber = studentInfo.getExaminerNumber();
        String idNumber = studentInfo.getIdNumber();
        StudentInfo result = iStudentInfoService.register(examinerNumber,idNumber);
        if (result!=null){
            String studentId = result.getStudentId();
            String password = idNumber;
            String name = result.getName();
            int row = iStudentService.insert(studentId,password,name);
            if (row>=1){
                Point point = new Point();
                point.setUserId(iStudentService.getUserId(studentId));
                iPointService.save(point);
            }
            return Result.success();
        }else {
            return Result.error("考生号或身份证号错误");
        }
    }

    // 登录
    @PostMapping("login")
    public Result login(@RequestBody Student student){
        String studentId = student.getStudentId();
        String password = student.getPassword();
        Student result = iStudentService.queryStudent(studentId,password);
        if (result!=null){
//            Map<String, Object> claims = new HashMap<>();
//            claims.put("studentId", result.getStudentId());
//            String token = JwtUtil.genToken(claims);
//            System.out.println("生成的"+token);
            return Result.success(result);
        }else {
            return Result.error("学号或密码错误");
        }
    }

    // 判断原密码是否正确
    @PostMapping("isPwdTrue")
    public Result isPwdTrue(@RequestBody Student student){
        String studentId = student.getStudentId();
        String password = student.getPassword();
        Student result = iStudentService.queryStudent(studentId,password);
        if (result!=null){
            return Result.success();
        }else {
            return Result.error("学号或密码错误");
        }
    }

    // 分页查询所有账号信息
    @GetMapping("listByPage")
    public MyResponseData listByPage(Student student){
        MyResponseData data = new MyResponseData();
        data.setErrorCode(0);
        data.setMessage("success");
        int row = iStudentService.queryTotal(student);
        System.out.println(row);
        data.setArg1(iStudentService.queryTotal(student));
        data.setList(iStudentService.listByPage(student));
        System.out.println(data.getArg1());
        return data;
    }

    // 删除账号
    @PostMapping("delete")
    public MyResponseData deleteMoreAccount(Integer[] ids){
        int row = iStudentService.deleteMoreAccount(ids);
        MyResponseData data = new MyResponseData();
        if (row>=1){
            data.setErrorCode(0);
            data.setMessage("success");
            data.setArg1(row);
        }else {
            data.setErrorCode(1);
            data.setMessage("error");
        }
        return data;
    }

    // 更新账号状态
    @PatchMapping("update")
    public Result updateStatus(Student student){
        Integer id = student.getId();
        String accountStatus = student.getAccountStatus();
        int row = iStudentService.updateStatus(id,accountStatus);
        if (row>=1){
            return Result.success();
        }else {
            return Result.error("操作失败");
        }
    }

    // 修改密码
    @PatchMapping("updatePwd")
    public Result updatePwd(@RequestBody Student student){
        iStudentService.updatePwd(student);
        return Result.success();
    }

    // 修改头像
    @PatchMapping("updateAvatar")
    public Result updateAvatar(Student student){
        iStudentService.updateById(student);
        return Result.success();
    }
}
